﻿Public Class SolucionConsultaFarmaciaDAO
    Public Shared Function insertarSolucionConsultaFarmacia(ByVal ConsultaFarmaciaId As Integer, ByVal SolucionConsultaFarmaciaFecha As DateTime, ByVal SolucionConsultaFarmaciaDescripcion As String, ByVal EmpleadoId As Integer, ByVal SolucionConsultaFarmaciaConformidad As Boolean) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            Constante.esNulo(SolucionConsultaFarmaciaDescripcion)
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_SolucionConsultaFarmacia"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.INSERTAR).DbType = DbType.Int32
                    .AddWithValue("@SolucionConsultaFarmaciaId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@ConsultaFarmaciaId", ConsultaFarmaciaId).DbType = DbType.Int32
                    .AddWithValue("@SolucionConsultaFarmaciaFecha", SolucionConsultaFarmaciaFecha).DbType = DbType.DateTime
                    .AddWithValue("@SolucionConsultaFarmaciaDescripcion", SolucionConsultaFarmaciaDescripcion).DbType = DbType.String
                    .AddWithValue("@EmpleadoId", EmpleadoId).DbType = DbType.Int32
                    .AddWithValue("@SolucionConsultaFarmaciaConformidad", SolucionConsultaFarmaciaConformidad).DbType = DbType.Boolean
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerSolucionesConsultasFarmacias(ByVal SolucionConsultaFarmaciaId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_SolucionConsultaFarmacia_obtener", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@SolucionConsultaFarmaciaId", SolucionConsultaFarmaciaId).DbType = DbType.Int32
            da.Fill(ds, "SolucionesConsultasFarmacias")

            Return ds.Tables("SolucionesConsultasFarmacias")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerCantidadSolucionesDeConsulta(ByVal ConsultaFarmaciaId As Integer) As Integer
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            Dim filas As Integer
            filas = 0
            With consulta
                .Connection = cn
                .CommandText = "Select Count(*) From SolucionConsultaFarmacia Where ConsultaFarmaciaId = " + ConsultaFarmaciaId.ToString
                filas = .ExecuteScalar()
            End With

            Return filas
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function actualizarSolucionConsultaFarmacia(ByVal SolucionConsultaFarmaciaId As Integer, ByVal ConsultaFarmaciaId As Integer, ByVal SolucionConsultaFarmaciaFecha As DateTime, ByVal SolucionConsultaFarmaciaDescripcion As String, ByVal EmpleadoId As Integer, ByVal SolucionConsultaFarmaciaConformidad As Boolean) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            Constante.esNulo(SolucionConsultaFarmaciaDescripcion)
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_SolucionConsultaFarmacia"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ACTUALIZAR).DbType = DbType.Int32
                    .AddWithValue("@SolucionConsultaFarmaciaId", SolucionConsultaFarmaciaId).DbType = DbType.Int32
                    .AddWithValue("@ConsultaFarmaciaId", ConsultaFarmaciaId).DbType = DbType.Int32
                    .AddWithValue("@SolucionConsultaFarmaciaFecha", SolucionConsultaFarmaciaFecha).DbType = DbType.DateTime
                    .AddWithValue("@SolucionConsultaFarmaciaDescripcion", SolucionConsultaFarmaciaDescripcion).DbType = DbType.String
                    .AddWithValue("@EmpleadoId", EmpleadoId).DbType = DbType.Int32
                    .AddWithValue("@SolucionConsultaFarmaciaConformidad", SolucionConsultaFarmaciaConformidad).DbType = DbType.Boolean
                End With
                .Connection = cn
                .ExecuteNonQuery()

            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function eliminarSolucionConsultaFarmacia(ByVal SolucionConsultaFarmaciaId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_SolucionConsultaFarmacia"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ELIMINAR).DbType = DbType.Int32
                    .AddWithValue("@SolucionConsultaFarmaciaId", SolucionConsultaFarmaciaId).DbType = DbType.Int32
                    .AddWithValue("@ConsultaFarmaciaId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@SolucionConsultaFarmaciaFecha", Constante.FECHAVACIA).DbType = DbType.DateTime
                    .AddWithValue("@SolucionConsultaFarmaciaDescripcion", Constante.VACIO).DbType = DbType.String
                    .AddWithValue("@EmpleadoId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@SolucionConsultaFarmaciaConformidad", False).DbType = DbType.Boolean
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With

            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerDescripcionSolucion(ByVal ConsultaFarmaciaId As Integer) As String
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            Dim id As Integer = -1
            Dim descripcion As String = ""
            With consulta
                .Connection = cn
                .CommandText = "Select MAX(SolucionConsultaFarmaciaId) From SolucionConsultaFarmacia Where ConsultaFarmaciaId = " + ConsultaFarmaciaId.ToString
                id = .ExecuteScalar()
                .CommandText = "Select SolucionConsultaFarmaciaDescripcion From SolucionConsultaFarmacia Where SolucionConsultaFarmaciaId = " + id.ToString()
                descripcion = .ExecuteScalar()
            End With

            Return descripcion
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function selectSolucionesFarmacia(ByVal ConsultaFarmaciaId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_SolucionConsultaFarmacia_select", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@ConsultaFarmaciaId", ConsultaFarmaciaId).DbType = DbType.Int32
            da.Fill(ds, "SolucionesConsultasFarmacias")

            Return ds.Tables("SolucionesConsultasFarmacias")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

End Class
